<?php

	class WebAdmin {

		private static $basedir = "./modules/";

		public static function createModule($params) {

			$module_name = $params[1];
			$default_controller = "default";

			if (isset($params[2]))
				$default_controller = $params[2];

			$ret = "";

			$ret .= WebAdmin::createDir($module_name);
			$ret .= WebAdmin::createDir($module_name . "/templates");
			$ret .= WebAdmin::createDir($module_name . "/controllers");
			$ret .= WebAdmin::createDir($module_name . "/areas");
			$ret .= WebAdmin::createDir($module_name . "/includes");
			$ret .= WebAdmin::createDir($module_name . "/includes/images");
			$ret .= WebAdmin::createDir($module_name . "/includes/js");
			$ret .= WebAdmin::createDir($module_name . "/includes/css");

			$ret .= WebAdmin::createController($module_name, $default_controller);

			$ret .= "\nCréation du module '" . $module_name . "' terminée.\n";

			return $ret;

		}

		public static function createController($module, $controller) {

			$filename = WebAdmin::$basedir . $module . "/controllers/" . $controller . ".php";

			$file = fopen($filename, "a+");

			fputs($file, "<?php");
			fputs($file, "\n\n");
			fputs($file, "\tclass " . $controller . "Ctrl extends PwController {");
			fputs($file, "\n");
			fputs($file, "\n");
			fputs($file, "\n");
			fputs($file, "\n");
			fputs($file, "\n");
			fputs($file, "\t}");
			fputs($file, "\n");
			fputs($file, "?>");

			fclose($file);

			return "Création du controlleur " . $controller . " dans le module " . $module . "\n";

		}

		public static function createDir($path) {


			$s = WebAdmin::$basedir . $path;
			mkdir($s, 0777);

			return "Création du répertoire " . $path . "\n";

		}
		
		
		
		
		/// Commandes pour le module Auth
		public static function addCredential($credential) {
			
			if (!in_array('auth', $GLOBALS['conf']->active_plugins)) return "Le plugin Auth ne semble pas activé";
			
			$c = new credentialModel();
			$c->name = $credential;
			
			if ($c->insert())
				return "Insertion du droit " . $credential . "\n";
			
			return "Impossible de créer le droit " . $credential . "\n";
		}
		
		public static function createGroup($groupname) {
			
			if (!in_array('auth', $GLOBALS['conf']->active_plugins)) return "Le plugin Auth ne semble pas activé";
			
			$groupname[0] = "";
			
			$groupname = implode(" ", $groupname);
			
			$g = new groupsModel();
			$g->groupname = $groupname;
			
			if ($g->insert()) return "Création du groupe " . $groupname . "\n";
			return "Impossible de créer le groupe " . $groupname . "\n";
			
		}
		
		
		public static function createUser($login, $pwd) {
			
			if (!in_array('auth', $GLOBALS['conf']->active_plugins)) return "Le plugin Auth ne semble pas activé";

			PwUser::add($login, $pwd);
			return "Création de l'utilisateur " . $login . "\n";
		}
		
		public static function addGroupCredential($chaine) {
			
			if (!in_array('auth', $GLOBALS['conf']->active_plugins)) return "Le plugin Auth ne semble pas activé";
			
			$chaine[0] = null;
			$chaine = explode(" ", $chaine);
			$chaine = split(":",$chaine);
			
			$g = new groupsModel();
			$gid = $g->getId($groupname);
			
			$c = new credentialModel();
			$c->name = $chaine[1];
			$cid = $c->get(array('name'));
			
			PwGroup::addCredential($gid, $cid->id);
			
			return "Ajout du droit '" . $chaine[1] . "' au groupe " . $chaine[0] . "\n";
		}

	}

?>
